<template>
  <div class="app-container">
    <div class="main">
      <div class="filter-container g-bgc-fill-el-input">
        <div class="filter-left">
          <!-- <el-select v-model="listQuery.founder" placeholder="创建人" clearable @change="comHandleFilterSelectChange"
            style="width: 122px" class="filter-item">
            <el-option v-for="(item, index) in createByOptions" :key="'cr' + index" :label="item.menufacturer"
            :value="item.id" />
          </el-select> -->
          <el-date-picker v-model="listQuery.date" type="daterange" @change="comHandleFilterDateChange"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" style="width: 208px" range-separator=""
            start-placeholder="开始时间" end-placeholder="结束时间">
          </el-date-picker>
          <np-input-search v-model="listQuery.title" placeholder="搜索问题标题" style="width: 360px;"
            @all-search="comHandleFilterAllSearch" />
        </div>
        <div class="filter-right">
          <el-button type="warning" @click="handleNewAddProblem">新增问题</el-button>
        </div>
      </div>

      <el-table class="g-el-table" :data="list" v-loading="listLoading">
        <el-table-column label="问题标题" prop="title" width="200PX">
          <div slot-scope="{row}">
            {{ row.title }}
          </div>
        </el-table-column>
        <el-table-column label="创建人" width="120px">
          <template slot-scope="{row}">
            <span>{{ row.createBy }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" min-width="150px">
          <template slot-scope="{row}">
            <span>{{ row.createDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="230" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleTopUp(row)">置顶</el-button>
            <el-button type="text" @click="handleDel(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <np-pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize"
        @pagination="getList" />
    </div>

    <ListEditAddProblemDialog :visible.sync="dialogEditAddProblemVisible" :info-row="editAddInfoRow"
      @editAddSuccessChange="editAddSuccessChange" />
  </div>
</template>

<script>
import { getMenufacturers } from "@/api/family-and-users-management/index"
import { fetchList, delHelp, UpdateHelpTop } from '@/api/help-management/list'
import ListEditAddProblemDialog from "./components/ListEditAddProblemDialog.vue"

export default {
  name: 'ComplexTable',
  components: { ListEditAddProblemDialog },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,

        founder: undefined, // 创建人 没有这个字段
        title: undefined,

        startDate: undefined,
        endDate: undefined,
      },
      brandOptions: [],
      createByOptions: [],

      // 编辑添加问题弹窗参数
      editAddInfoRow: null,
      dialogEditAddProblemVisible: false,
    }
  },
  created() {
    this.getList()
    // 获取品牌
    getMenufacturers({ pageNum: 1, pageSize: 1000 }).then(res => {
      if (res.code === 200) {
        this.brandOptions = res.data.list || []
      }
    })
        // 获取品牌
    getMenufacturers({ pageNum: 1, pageSize: 1000 }).then(res => {
      if (res.code === 200) {
        this.brandOptions = res.data.list || []
      }
    })
  },
  methods: {
    getList() {
      this.listLoading = true
      let pans = { ...this.listQuery }
      delete pans.date
      fetchList(this.listQuery).then(res => {
        if (res.code === 200) {
          this.list = res.data.list
          this.total = res.data.total
        }

        // Just to simulate the time of the request
        this.listLoading = false
      })
    },
    comHandleFilterAllSearch(e, type) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterSelectChange(e) {
      this.listQuery.pageNum = 1
      this.getList()
    },
    comHandleFilterDateChange(e) {
      this.listQuery.pageNum = 1
      this.listQuery.startDate = e ? e[0] : ''
      this.listQuery.endDate = e ? e[1] : ''
      this.getList()
    },
    editAddSuccessChange(e) {
      // e 判断是编辑 edit 还是添加 add
      if (e === 'add') {
        this.getList()
        return
      }
      if (e === 'edit') {
        this.getList()
      }
    },
    handleNewAddProblem() {
      this.editAddInfoRow = null
      this.dialogEditAddProblemVisible = true
    },
    handleEdit(row) {
      this.editAddInfoRow = row
      this.dialogEditAddProblemVisible = true
    },
    handleTopUp(row) {
      let params = {
        id: row.id || '',
      }
      UpdateHelpTop(params).then(res => {
        if (res.code === 200) {
          this.$message.success(`置顶成功!`)
          this.listQuery.pageNum = 1
          this.getList()
          return
        }
        this.$message.error('置顶失败。')
      }).catch(err => {
        this.$message.error('系统错误，服务失败。')
      })
    },
    handleDel(row) {
      let params = {
        id: row.id || '',
        path: row.filePath || '',
      }
      delHelp(params).then(res => {
        if (res.code === 200) {
          this.$message.success(`删除成功!`)
          this.getList()
          return
        }
        this.$message.error('删除失败。')
      }).catch(err => {
        this.$message.error('系统错误，服务失败。')
      })
    },
  }
}
</script>
<style scoped lang="scss">
.app-container {
  height: 100%;
  overflow: auto;
}

.main {
  background-color: #fff;
  border-radius: 16px;
  padding: 20px 20px 28px;
}
</style>